	*cd "C:\Users\msj22\Dropbox\research\wrongful_discharge\replication_May_2022\intermediate_dtas"
	cd /Users/dschwab/Dropbox/wrongful_discharge/replication_May_2022/intermediate_dtas

	global final 2005

*Right to work data
//	import excel "${rtw_data}right_to_work_states.xlsx", sheet("Sheet1") firstrow clear
	import excel "../data/right_to_work_states.xlsx", sheet("Sheet1") firstrow clear	
	gen year_min = min(yearconstitutionamended, YearStatuteEnacted)
	keep if year_min<=1980 // only states that became RTW prior to 2007
	keep state_code 
	rename state_code state2
	gen right_to_work = 1
	tempfile rtw
	save `rtw', replace
	save tmp, replace
	
*Main dataset--reshape long so that union/non-union/overall are separate rows 

	eststo clear
	use osha_nsc2, clear
	
	
	keep if year <= $final

	global sec_samp "(sector == 1)"
	global sample = "((state_run_office!=1 & year>=1979) | (state_run_office==1 & year>=1992)) & $sec_samp" 

	keep if $sample 
	
	
	global policies = "wb wc wdlapY wdlagY wdlacY"

/*	reghdfe ln_acc_rateY_matt comm $policies $weight , absorb(year_div statesec statesec#c.year ) vce(cluster state_code)

	reghdfe total_viol_progY comm $policies $weight , absorb(year_div statesec statesec#c.year ) vce(cluster state_code)
	
	
	reghdfe ln_acc_rateY_matt comm $policies $weight , absorb($year_area $fe) vce(cluster state_code)

	reghdfe ln_acc_rateY_matt comm $policies $weight , absorb($year_area statesec statesec#c.year) vce(cluster state_code) 
*/
	
	
	keep ln_acc_rateY_matt ln_*union_acc_rateY_matt $policies statesec state2 year secyear year_div   unemp ln_prgm_rateY_matt dem_gov sh_emp1979 state_run_office region 
			
	rename ln_acc_rateY_matt ln_acc_rateY_matt_overall
	rename ln_union_acc_rateY_matt ln_acc_rateY_matt_union
	rename ln_nonunion_acc_rateY_matt ln_acc_rateY_matt_nonunion


			
	*Reshape data long
		reshape long ln_acc_rateY_matt,  i(statesec state2 year) j(union_sector) string
	*Merge in RTW data
		merge m:1 state2 using `rtw', keep(master match) nogen
	
		replace right_to_work = 0 if right_to_work==.

		gen union_yes = union_sector=="_union"
		lab var union_yes "Unionized workplaces"

		gen not_rtw = right_to_work!=1
		lab var not_rtw "No Right-to-Work law"
	*Run regressions
		eststo clear

	global trend = "c.year#i.state_code"
	global trend_label = "Yes"
	global year_area = "year_div" 
	global yr_label = "Yes" 
		
	global tran_label = "ln"
	global depvar = "ln_acc_rateY_matt"
	global weight = "[aw = sh_emp1979]"
	global fe = "statesec secyear"
	

	**************************
	*Quick program for saving regression results
	capture program drop estoutme 
	program define estoutme
	syntax , [depvar(str)]
	
		estadd local basicFE "Yes"
		qui sum `e(depvar)' if e(sample)  
		estadd scalar meandv = r(mean)
	
	end 
	*************************

	local policies wdlapY wb wc 
	local policies wdlapY 
	local other_pols_wdlapY wb wc 
	local other_pols_wb wdlapY wc 
	local other_pols_wc wdlapY wb 

	estimates clear 
	
	foreach policy in `policies' {
	    
		*Union/non-union 
			*just state-year FE  
				reghdfe $depvar c.`policy'##i1.union_yes  if union_sector!="_overall"  $weight , absorb(year#union_yes statesec#union_yes  ) vce(cluster statesec)
					estimates store `policy'_un_basic
					estoutme 
					estadd local region_year "No" 			
					estadd local state_trend "No"
					estadd local controls "No"
			*+ div-year FE 
				reghdfe $depvar c.`policy'##i1.union_yes   if union_sector!="_overall"  $weight , absorb(year_div#union_yes statesec#union_yes ) vce(cluster statesec)
					estimates store `policy'_un_dy
					estoutme 
					estadd local region_year "Yes" 			
					estadd local state_trend "No"
					estadd local controls "No"
			*+ state trends 
				reghdfe $depvar c.`policy'##i1.union_yes   if union_sector!="_overall"  $weight , absorb(year_div#union_yes statesec#union_yes union_yes#statesec#c.year ) vce(cluster statesec)
					estimates store `policy'_un_tr
					estoutme 
					estadd local region_year "Yes" 			
					estadd local state_trend "Yes"
					estadd local controls "No"
			*+ controls 
				reghdfe $depvar c.`policy'##i1.union_yes  ln_prgm_rateY_matt `other_pols_`policy'' wdlagY wdlacY if union_sector!="_overall"  $weight , absorb(year_div#union_yes statesec#union_yes union_yes#statesec#c.year ) vce(cluster statesec)
					estimates store `policy'_un_ctrl
					estoutme 
					estadd local region_year "Yes" 			
					estadd local state_trend "Yes"
					estadd local controls "Yes"
	
		*RTW / not 
		*First, run full specification to get conisstent sample across columns 
					qui reghdfe $depvar c.`policy'##i1.not_rtw  ln_prgm_rateY_matt `other_pols_`policy''  wdlagY wdlacY if union_sector=="_overall"  $weight , absorb(year_div#not_rtw statesec statesec#c.year ) vce(cluster statesec)
						gen in_sample_nsc = e(sample)==1
			*just state-year FE  
				reghdfe $depvar c.`policy'##i1.not_rtw   if union_sector=="_overall" & in_sample_nsc==1 $weight , absorb(year#not_rtw statesec) vce(cluster statesec)
					estimates store `policy'_rtw_basic
					estoutme 
					estadd local region_year "No" 			
					estadd local state_trend "No"
					estadd local controls "No"
			*+ div-year FE 
				reghdfe $depvar c.`policy'##i1.not_rtw   if union_sector=="_overall" & in_sample_nsc==1 $weight , absorb(year_div#not_rtw statesec) vce(cluster statesec)
					estimates store `policy'_rtw_dy
					estoutme 
					estadd local region_year "Yes" 			
					estadd local state_trend "No"
					estadd local controls "No"
			*+ state trends 
				reghdfe $depvar c.`policy'##i1.not_rtw   if union_sector=="_overall" & in_sample_nsc==1 $weight , absorb(year_div#not_rtw statesec statesec#c.year ) vce(cluster statesec)
					estimates store `policy'_rtw_tr
					estoutme 
					estadd local region_year "Yes" 			
					estadd local state_trend "Yes"
					estadd local controls "No"
			*+ controls 
				reghdfe $depvar c.`policy'##i1.not_rtw  ln_prgm_rateY_matt `other_pols_`policy''  wdlagY wdlacY if union_sector=="_overall" & in_sample_nsc==1 $weight , absorb(year_div#not_rtw statesec statesec#c.year ) vce(cluster statesec)
					estimates store `policy'_rtw_ctrl
					estoutme 
					estadd local region_year "Yes" 			
					estadd local state_trend "Yes"
					estadd local controls "Yes"
			cap drop in_sample_nsc
		} // foreach policy 

		foreach policy in `policies' {
			esttab `policy'*   using "../regression_results/union_rtw_`policy'.tex", ///
			booktabs fragment replace /// 			
			scalars("meandv Mean Dep Var" "basicFE state and year FE" "region_year Division-year FE" "state_trend State trend"  "controls Additional Controls") ///
			se b(3) star(* .1 ** .05 *** .01)   label nodepvar ///
			keep(*`policy'* ln_prgm_rateY_matt `other_pols_`policy'')  order(*`policy'* ln_prgm_rateY_matt `other_pols_`policy'') ///
			nonotes nomtitles  	
		}
	
	

	
	
	
	
if 1==1 { //summary stats for RTW and non-RTW
	use osha_nsc2, clear
	merge m:1 state2 using tmp, keep(master match) nogen
	
	replace right_to_work = 0 if right_to_work==.

	gen not_rtw = right_to_work!=1
	lab var not_rtw "No Right-to-Work law"

	keep if year <= $final
	
	gen comp_rate = num_complaint_inspY / empY

	label var comp_rate "Complaint inspections per 1000 employees (OSHA)"
	label var acc_rateY_matt "Accident inspections per 1000 employees (OSHA)"
	label var death_rate "Deaths per 1000 employees (NSC)"
	label var wdlapY "Public policy exception"
	label var wc "Workers' comp. anti-retaliation statute"
	label var wb "Whistleblower protection statute"
	local vars = "comp_rate acc_rateY_matt death_rate wdlapY wc wb" 
	
	gen not_rtw_s = "RTW"
	replace not_rtw_s = "Not RTW" if not_rtw == 1
	
	sort not_rtw
	estpost tabstat `vars', by(not_rtw_s) statistics(mean sd) columns(statistics) listwise
	esttab using "../regression_results/sum_rtw.tex", replace fragment label main(mean) aux(sd) nostar unstack noobs nonote  nonumber nomtitle
}	


